<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/

	class Auth extends Admin_Controller
	{

		public function __construct()
		{
			parent::__construct();

            $this->load->model("backend/user_model", "MUser", TRUE);

		}

        public function login()
        {
            if($this->my_session->is_Admin())
            {
                redirect(base_url()."backend/layout");

                exit();
            }

            /*Validate data*/
            $this->form_validation->set_rules('uname','Tên đăng nhập','trim|required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('pass','Mật khẩu','trim|required|min_length[3]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('email','Email','trim|required|min_length[3]|valid_email|xss_clean');
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s không được ít hơn 3 ký tự');
            $this->form_validation->set_message('max_length','%s không được nhiều hơn 20 ký tự');
            $this->form_validation->set_message('valid_email','%s không đúng');

            if($this->form_validation->run()==FALSE)
            {
                $data['msg'] = '';
            }
            else
            {
                $username = $this->input->post('uname');
                $password = $this->input->post('pass');
                $email = $this->input->post('email');

                $check = $this->MUser->checkLoginAdmin($username, $password, $email);

                if(!$check)
                {
                    $data['msg'] = '<span style="font-weight:bold">Tên đăng nhập</span> hoặc<span style="font-weight:bold"> Mật khẩu</span>  hoặc <span style="font-weight:bold">Email</span> không chính xác.';
                }
                else
                {
                    $session_admin = array(
                        'username'  => $check->admin_username,
                        'admin'     => $check->admin_fullname,
                        'id'        => $check->admin_id,
                        'loggedin'  => 1
                    );

                    $this->native_session->set_userdata('admin',$session_admin);

                    redirect(base_url()."backend/layout");

                    exit();
                }
            }//End else

            $this->load->view("backend/auth/login",$data);
        }/*End function login*/

        public function logout()
        {
            $this->native_session->unset_userdata('admin');

            redirect(base_url()."backend/auth/login");
        }

	}

/* End of file Auth.php */
/* Location: ./Controllers/auth.php */